package commons.base.rpc;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import commons.base.netty.http.HttpServer;
import commons.base.rpc.example.RpcServlet;

public class RpcServer {

	private static final Logger log = LogManager.getLogger(RpcServer.class);
	
	private HttpServer httpServer;
	private RpcServlet rpcServlet;
	
	public RpcServer(){
		rpcServlet = new RpcServlet();
	}
	
	public void addService(Class<?> interfaceClazz, Object impl){
		rpcServlet.addService(interfaceClazz, impl);
	}
	
	public void publish(int port){
		publish(port, "/rpc/.*");
	}
	public void publish(int port, String uri){
		httpServer = new HttpServer(port);
		httpServer.addServlet(uri, rpcServlet);
		httpServer.start(); 
		
		log.info("http://localhost:" + port + uri);
	}
	
	public void shutdown(){
		if(httpServer != null){
			httpServer.shutdown();
		}
	}
}
